package com.sentiment.dbutil;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.sentiment.database.bean.Keyword;
import com.sentiment.database.bean.UserInfo;
import com.sentiment.database.bean.WebsiteInfo;
import com.sentiment.database.dbUtil.DbUtil;

public class UtilDao {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UtilDao dao = new UtilDao();
//		dao.saveKeyword2user("56473a788bcdb018b8da117b", "北京d欢迎你");
		System.out.println();
//		dao.getKeywordList("56473a788bcdb018b8da117b");
//		dao.deleteKeywordFromuser("56473a788bcdb018b8da117b", "56483a158bcdb028c0dd9863");
//		dao.removeKeyword("56483a158bcdb028c0dd9863");
		WebsiteInfo website = new WebsiteInfo("url","6/23");
		dao.addWebsiteInfo("56473a788bcdb018b8da117b", website.toJSON().toString());
//		dao.deleteAllWebsite("56473a788bcdb018b8da117b");
//		dao.getWebsiteList("56473a788bcdb018b8da117b");
	}

	
	public ArrayList<Keyword> getKeywordList(String user_id){
		TreeMap<String,Integer> tree = new TreeMap();  

		ArrayList<Keyword> karr = new ArrayList();
		DbUtil dbu = new DbUtil();
		dbu.openConnection(UserInfo.dbName, UserInfo.collection);
		UserInfo uinfo = new UserInfo();
		uinfo.setUser_id(user_id);
		Document d = dbu.query(uinfo.toDocument(), new String[] { "user_id" });
		dbu.closeConnection();
		UserInfo uinfo2 = new UserInfo(d);
		Object o = uinfo2.getKeywordList();
		ArrayList<String> arr=null;
		if(o==null){
			arr = new ArrayList();
		}else{
			arr = (ArrayList<String>) o;
		}
		Keyword keyword =null;
		for(String s : arr){
			keyword = getKeywordByID(s);
			if(keyword == null){
				
			}else{
				if(tree.containsKey(keyword.getKeyword())){
					
				}else{
					tree.put(keyword.getKeyword(), 1);
					karr.add(keyword);
				}
				
			}
		}
		return karr;
		
	}
	
	public Keyword getKeywordByID(String _id){
		Keyword keyword =null;
		DbUtil dbu = new DbUtil();
		dbu.openConnection(Keyword.dbName, Keyword.collection);
		Document d = dbu.query(_id);
		if(d!=null){
			return new Keyword(d);
		}
		dbu.closeConnection();
		return keyword;
	}
	
	public void saveKeyword2user(String user_id,String keyword){
		DbUtil dbu2 = new DbUtil();
		dbu2.openConnection(Keyword.dbName, Keyword.collection);
		Date date = new Date();
		Keyword word = new Keyword(date,keyword,"社会");
		dbu2.insert(word.toDocument());
		Document wd = dbu2.query(word.toDocument(), new String[]{"date","keyword"});
		Keyword kw = new Keyword(wd);
		String keyword_id = kw.get_id();
		dbu2.closeConnection();
		DbUtil dbu = new DbUtil();
		dbu.openConnection(UserInfo.dbName, UserInfo.collection);
		UserInfo uinfo = new UserInfo();
		uinfo.setUser_id(user_id);
		Document d = dbu.query(uinfo.toDocument(), new String[] { "user_id" });
		UserInfo uinfo2 = new UserInfo(d);
		uinfo = new UserInfo(d);
		Object o = uinfo2.getKeywordList();
		ArrayList<String> arr=null;
		if(o==null){
			arr = new ArrayList();
		}else{
			arr = (ArrayList<String>) o;
		}
		ArrayList<String> arr3 =new ArrayList(arr);
		if(!arr3.contains(keyword_id)){
			arr3.add(keyword_id);
			uinfo2.setKeywordList(arr3);
			dbu.update(uinfo.toDocument(), new String[]{"keywordList"}, uinfo2.toDocument(), new String[]{"keywordList"});
//			dbu.remove(uinfo.toDocument(), new String[]{});
//			System.out.println(uinfo2.toString());
////			dbu.insert(uinfo2.toDocument());
		}
		dbu.closeConnection();
		
	}
	
	public void updateKeyword2User(String user_id,Keyword kword ){
		if(user_id == null){
			return;
		}
		DbUtil dbu3 = new DbUtil();
		dbu3.openConnection(Keyword.dbName, Keyword.collection);
		Document d = dbu3.query(kword.get_id());
		if(d!=null){
			dbu3.update(d, new String[]{"date","keyword","type"}, kword.toDocument(), new String[]{"date","keyword","type"});
		}
		dbu3.closeConnection();
	}
	
	public void deleteKeywordFromuser(String user_id,String keyword_id){
		
		DbUtil dbu = new DbUtil();
		dbu.openConnection(UserInfo.dbName, UserInfo.collection);
		UserInfo uinfo = new UserInfo();
		uinfo.setUser_id(user_id);
		Document d = dbu.query(uinfo.toDocument(), new String[] { "user_id" });
		UserInfo uinfo2 = new UserInfo(d);
		uinfo = new UserInfo(d);
		Object o = uinfo2.getKeywordList();
		ArrayList<String> arr=null;
		if(o==null){
			arr = new ArrayList();
		}else{
			arr = (ArrayList<String>) o;
		}
		ArrayList<String> arr3 =new ArrayList(arr);
		if(arr3.contains(keyword_id)){
			arr3.remove(keyword_id);
			uinfo2.setKeywordList(arr3);
			dbu.update(uinfo.toDocument(), new String[]{"keywordList"}, uinfo2.toDocument(), new String[]{"keywordList"});
//			dbu.remove(uinfo.toDocument(), new String[]{});
//			System.out.println(uinfo2.toString());
////			dbu.insert(uinfo2.toDocument());
		}
		dbu.closeConnection();
//		removeKeyword(keyword_id);
	}
	
	
	public void deleteAllWebsite(String user_id){
		DbUtil dbu = new DbUtil();
		dbu.openConnection(UserInfo.dbName, UserInfo.collection);
		UserInfo uinfo = new UserInfo();
		uinfo.setUser_id(user_id);
		Document d = dbu.query(uinfo.toDocument(), new String[] { "user_id" });
		UserInfo uinfo2 = new UserInfo(d);
		uinfo = new UserInfo(d);
		Object o = uinfo2.getWebsiteList();
		ArrayList<String> arr=null;
		if(o==null){
			arr = new ArrayList();
		}else{
			arr = (ArrayList<String>) o;
		}
		ArrayList<String> arr3 =new ArrayList();
		uinfo2.setWebsiteList(arr3);
		dbu.update(uinfo.toDocument(), new String[]{"websiteList"}, uinfo2.toDocument(), new String[]{"websiteList"});
		dbu.closeConnection();
		
		
		dbu.closeConnection();
	}
	public void updateWebsiteInfo(String user_id,String website){
		DbUtil dbu = new DbUtil();
		dbu.openConnection(UserInfo.dbName, UserInfo.collection);
		UserInfo uinfo = new UserInfo();
		uinfo.setUser_id(user_id);
		Document d = dbu.query(uinfo.toDocument(), new String[] { "user_id" });
		UserInfo uinfo2 = new UserInfo(d);
		uinfo = new UserInfo(d);
		Object o = uinfo2.getWebsiteList();
		ArrayList<String> arr=null;
		if(o==null){
			arr = new ArrayList();
		}else{
			arr = (ArrayList<String>) o;
		}
		ArrayList<String> arr3 =new ArrayList(arr);
		
		for(String str :arr3){
			WebsiteInfo webs = new WebsiteInfo(str);
			if(webs.getWebsite().equals("website")){
				arr3.remove(str);
				uinfo2.setWebsiteList(arr3);
				dbu.update(uinfo.toDocument(), new String[]{"websiteList"}, uinfo2.toDocument(), new String[]{"websiteList"});
				dbu.closeConnection();
				return;
			}
		}
		
		dbu.closeConnection();
	}
	
	public void deleteWebsiteInfo(String user_id,String website_date){
		DbUtil dbu = new DbUtil();
		dbu.openConnection(UserInfo.dbName, UserInfo.collection);
		UserInfo uinfo = new UserInfo();
		uinfo.setUser_id(user_id);
		Document d = dbu.query(uinfo.toDocument(), new String[] { "user_id" });
		UserInfo uinfo2 = new UserInfo(d);
		uinfo = new UserInfo(d);
		Object o = uinfo2.getWebsiteList();
		ArrayList<String> arr=null;
		if(o==null){
			arr = new ArrayList();
		}else{
			arr = (ArrayList<String>) o;
		}
		ArrayList<String> arr3 =new ArrayList(arr);
		if(arr3.contains(website_date)){
			System.out.println("kk"+website_date);
			arr3.remove(website_date);
			uinfo2.setWebsiteList(arr3);
			dbu.update(uinfo.toDocument(), new String[]{"websiteList"}, uinfo2.toDocument(), new String[]{"websiteList"});
//			dbu.remove(uinfo.toDocument(), new String[]{});
//			System.out.println(uinfo2.toString());
////			dbu.insert(uinfo2.toDocument());
		}
		dbu.closeConnection();
//		removeKeyword(keyword_id);
	}
	
	public ArrayList<WebsiteInfo> getWebsiteList(String user_id){
		ArrayList<WebsiteInfo> karr = new ArrayList();
		DbUtil dbu = new DbUtil();
		dbu.openConnection(UserInfo.dbName, UserInfo.collection);
		UserInfo uinfo = new UserInfo();
		uinfo.setUser_id(user_id);
		Document d = dbu.query(uinfo.toDocument(), new String[] { "user_id" });
		dbu.closeConnection();
		UserInfo uinfo2 = new UserInfo(d);
		Object o = uinfo2.getWebsiteList();
		ArrayList<String> arr=null;
		if(o==null){
			arr = new ArrayList();
		}else{
			
			arr = (ArrayList<String>) o;
		}
		WebsiteInfo website;
		for(String s : arr){
			website = new WebsiteInfo(s);
			System.out.println(website.toString());
			karr.add(website);
		}
		
		return karr;
	}
	
	public void addWebsiteInfos(String user_id,ArrayList<WebsiteInfo> websites){

		DbUtil dbu = new DbUtil();
		dbu.openConnection(UserInfo.dbName, UserInfo.collection);
		UserInfo uinfo = new UserInfo();
		uinfo.setUser_id(user_id);
		Document d = dbu.query(uinfo.toDocument(), new String[] { "user_id" });
		UserInfo uinfo2 = new UserInfo(d);
		uinfo = new UserInfo(d);
		Object o = uinfo2.getWebsiteList();
		ArrayList<String> arr=null;
		if(o==null){
			arr = new ArrayList();
		}else{
			arr = (ArrayList<String>) o;
		}
		ArrayList<String> arr3 =new ArrayList();
		System.out.println(arr3.toString());
		
		for(WebsiteInfo webinfo : websites){
			arr3.add(webinfo.toJSON());
		}
		uinfo2.setWebsiteList(arr3);
		dbu.update(uinfo.toDocument(), new String[]{"websiteList"}, uinfo2.toDocument(), new String[]{"websiteList"});
		dbu.closeConnection();
		
		
		dbu.closeConnection();
	}
	public void addWebsiteInfo(String user_id,String website_date){
		System.out.println(website_date);

		DbUtil dbu = new DbUtil();
		dbu.openConnection(UserInfo.dbName, UserInfo.collection);
		UserInfo uinfo = new UserInfo();
		uinfo.setUser_id(user_id);
		Document d = dbu.query(uinfo.toDocument(), new String[] { "user_id" });
		UserInfo uinfo2 = new UserInfo(d);
		uinfo = new UserInfo(d);
		Object o = uinfo2.getWebsiteList();
		ArrayList<String> arr=null;
		if(o==null){
			arr = new ArrayList();
		}else{
			arr = (ArrayList<String>) o;
		}
		ArrayList<String> arr3 =new ArrayList(arr);
		System.out.println(arr3.toString());
		System.out.println(website_date);
		if(!arr3.contains(website_date)){
			arr3.add(website_date);
			uinfo2.setWebsiteList(arr3);
			dbu.update(uinfo.toDocument(), new String[]{"websiteList"}, uinfo2.toDocument(), new String[]{"websiteList"});
			dbu.closeConnection();
			return;
		}
		
		dbu.closeConnection();
	}
	public void removeKeyword(String _id){
		DbUtil dbu3 = new DbUtil();
		dbu3.openConnection(Keyword.dbName, Keyword.collection);
		Keyword kdf = new Keyword();
		kdf.set_id(_id);
		
//		dbu3.remove(kdf.toDocument(), new String[]{});
		dbu3.closeConnection();
	}
}
